package chuangyi.com.org.ViewModel;

import android.content.Context;
import android.databinding.Bindable;
import android.view.View;

import com.solidfire.gson.internal.LinkedTreeMap;

import java.util.ArrayList;
import java.util.List;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.DBManager;
import chuangyi.com.org.DOMIHome.NotifyParentCheckedChanged;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.Utils.Utils;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.adapter.ListBindingAdapter;
import chuangyi.com.org.DOMIHome.adapter.XListAdapterInterface;

/**
 * Created by zxh on 16/12/11.
 */

public class SearchModel extends XListModel implements NotifyParentCheckedChanged {
    //    private List<CheckedMenuItem>
    private String searchText;
    private ListBindingAdapter historyList;
    private ListBindingAdapter hotSearch;
    private ListBindingAdapter<CheckedMenuItem> searchTypes;
    private boolean searched;
    private String searchType;

    @Bindable
    public String getSearchText() {
        return searchText;
    }

    public void setSearchText(String searchText) {
        this.searchText = searchText;
        notifyPropertyChanged(BR.searchText);
    }

    @Bindable
    public boolean isSearched() {
        return searched;
    }

    public void setSearched(boolean searched) {
        this.searched = searched;
        notifyPropertyChanged(BR.searched);
    }

    @Bindable
    public ListBindingAdapter getHistoryList() {
        return historyList;
    }

    @Bindable
    public ListBindingAdapter getHotSearch() {
        return hotSearch;
    }

    @Bindable
    public ListBindingAdapter<CheckedMenuItem> getSearchTypes() {
        return searchTypes;
    }
//
//    @Bindable
//    public int getSelectedType() {
//        return searchTypes;
//    }
//
//    public void setSelectedType(int selectedType) {
//        this.selectedType = selectedType;
//        if (searched) {
//            setCurrentPage(1);
//            load(1);
//        }
//        notifyPropertyChanged(BR.selectedType);
//    }

    public SearchModel(Context context) {
        super(context);
        super.getHandler().setTitle("搜索");
        historyList = new ListBindingAdapter(getContext(), BR.record);
        hotSearch = new ListBindingAdapter(getContext(), BR.record);
        getAdapter().setVariableId(BR.item);
        initSearchType();
        init();
    }

    private void initSearchType() {
        searchTypes = new ListBindingAdapter<CheckedMenuItem>(getContext(), BR.menu);
        CheckedMenuItem type = new CheckedMenuItem(getContext(), R.layout.checked_menu);
        type.setTitle("全部");
        type.setCode("all");
        type.setChecked(true);
        type.setParent(this);
        searchType = "all";
        searchTypes.add(type);

        type = new CheckedMenuItem(getContext(), R.layout.checked_menu);
        type.setTitle("视频");
        type.setCode("v");
        type.setParent(this);
        type.setChecked(false);
        searchTypes.add(type);

        type = new CheckedMenuItem(getContext(), R.layout.checked_menu);
        type.setTitle("文章");
        type.setCode("w");
        type.setParent(this);
        type.setChecked(false);
        searchTypes.add(type);

//        type = new CheckedMenuItem(getContext(), R.layout.checked_menu);
//        type.setTitle("其他");
//        type.setCode("other");
//        type.setParent(this);
//        type.setChecked(false);
//        searchTypes.add(type);
    }

    private void init() {
        List<SearchRecord> recordList = DBManager.getInstance(getContext()).querySearchRecord();

        for (SearchRecord record : recordList) {
            record.setContext(getContext());
            record.setLayout(R.layout.i_search_history);
            record.setParent(this);
            historyList.add(record);
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                ArrayList list = WebApi.hotSearch(getContext());
                if (list == null)
                    return;
                int index = 1;
                for (Object item : list) {
                    LinkedTreeMap map = (LinkedTreeMap) item;
                    SearchRecord record = new SearchRecord(getContext(), R.layout.i_search_hot);
                    record.setText((String) map.get("title"));
                    record.setNo(index);
                    record.setParent(SearchModel.this);
                    hotSearch.add(record);
                    index++;
                }
                notifyDataSetChanged(hotSearch);
            }
        }).start();
    }

    /**
     * 点击搜索
     *
     * @param view
     */
    public void onClickSearch(View view) {
        if (Utils.isNullOrEmpty(searchText)) {
            showToast("请输入搜索内容");
            return;
        }
        setCurrentPage(1);
        searched = true;
        DBManager.getInstance(getContext()).updateSearchRecord(searchText);
        load(1);
    }

    /**
     * 清空搜索历史
     *
     * @param view
     */
    public void onClickClear(View view) {
        DBManager.getInstance(getContext()).clearSearchRecord();
        historyList.clear(true);
    }

    @Override
    public void notifyCheckedChanged(CheckedMenuItem menu) {
        if (menu.isChecked()) {
            if (searchType != null && !searchType.equals(menu.getCode())) {
                searchType = menu.getCode();

                if (searched == false) {
                    return;
                }
                getAdapter().clear(true);
                notifyDataSetChanged(getAdapter());
                setCurrentPage(1);
                load(1);
            }
        }
    }

    @Override
    public void load(final int page) {
//        String type = "all";
//        for (int i = 0; i < searchType.size(); i++) {
//            if (searchType.getI(i).isChecked()) {
//                type = searchType.get(i).getCode();
//            }
//        }
//        final String selectedType = type;

        if (!searched) {
            return;
        }
        setNoMore();
        final XListAdapterInterface adapter = getAdapter();
        adapter.clear(true);
        setSearched(true);
        new Thread(new Runnable() {
            @Override
            public void run() {
                final ArrayList list = WebApi.search(getContext(), searchText, searchType);
                try {
                    for (Object item : list) {
                        LinkedTreeMap map = (LinkedTreeMap) item;
/*文章 "articleid":"1","title":"测试 ","context":"","nickname":"15022189533","click":"1","type":"w"
* 视频 "id":"1","title":"视频1","context":"视频介绍视频介绍","nickname":"18622536910","click":"0","type":"v"
* 其他 "questionid":"1","title":"123","questiondesc":"上述事实上还是","nickname":"156****1314","click":"11","type":""
* 全部 {"id":"9","title":"123","context":"","nickname":"","click":"0","type":"w"} */
                        SearchItem searchItem = new SearchItem(getContext(), R.layout.i_search, searchType);
                        String itemType = (String) map.get("type");
                        if (itemType != null) {
                            searchItem.setType(itemType);
                        }
                        switch (searchType) {
                            case "all":
                                searchItem.setId(Utils.getIntValue(map.get("id")));
                                searchItem.setImg((String)map.get("avatar"));
                                searchItem.setTitle((String) map.get("title"));
                                searchItem.setContent((String) map.get("context"));
                                searchItem.setNickName((String) map.get("nickname"));
                                searchItem.setClick(Utils.getIntValue(map.get("click")));
                                break;
                            case "w":
                                searchItem.setId(Utils.getIntValue(map.get("articleid")));
                                searchItem.setImg((String)map.get("avatar"));
                                searchItem.setTitle((String) map.get("title"));
                                searchItem.setContent((String) map.get("context"));
                                searchItem.setNickName((String) map.get("nickname"));
                                searchItem.setClick(Utils.getIntValue(map.get("click")));
                                break;
                            case "v":
                                searchItem.setImg((String)map.get("avatar"));
                                searchItem.setId(Utils.getIntValue(map.get("id")));
                                searchItem.setTitle((String) map.get("title"));
                                searchItem.setContent((String) map.get("context"));
                                searchItem.setNickName((String) map.get("nickname"));
                                searchItem.setClick(Utils.getIntValue(map.get("click")));
                                break;
                            case "other":
                                searchItem.setId(Utils.getIntValue(map.get("questionid")));
                                searchItem.setTitle((String) map.get("title"));
                                searchItem.setContent((String) map.get("questiondesc"));
                                searchItem.setNickName((String) map.get("nickname"));
                                searchItem.setClick(Utils.getIntValue(map.get("click")));
                                break;
                        }
                        adapter.add(searchItem);
                    }
                    if (list.size() > 0) {
                        setCurrentPage(page);
                    }
                } catch (Exception ex) {
                    showToast("解析数据时发生错误");
                }
                notifyDataSetChanged(list);
            }
        }).start();
    }
}
